<?php

namespace app\common\model\user;

use app\common\model\BaseModel;


class Sn extends BaseModel
{
    protected $name = 'user_sn';

    /**
     * 关联会员记录表
     * @return \think\model\relation\BelongsTo
     */
    public function user()
    {
        $module = self::getCalledModule() ?: 'common';
        return $this->belongsTo("app\\{$module}\\model\\User", 'user_id', 'user_id');
    }

    /**
     * 性别
     * @param $value
     * @return mixed
     */
    public function getGenderAttr($value)
    {
        $status = [0 => '位置', 1 => '男', 2 => '女'];
        return ['text' => $status[$value], 'value' => $value];
    }

    /**
     * 新增记录
     * @param $data
     */
    public static function add($data)
    {
        $static = new static;
        $static->save(array_merge(['wxapp_id' => $static::$wxapp_id], $data));
    }

    /**
     * 新增记录 (批量)
     * @param $saveData
     * @return array|false
     * @throws \Exception
     */
    public function onBatchAdd($saveData)
    {
        return $this->isUpdate(false)->saveAll($saveData);
    }

    /**
     * 详情
     * @param $id
     * @return null|static
     * @throws \think\exception\DbException
     */
    public static function detail($id)
    {
        return self::get($id, ['user']);
    }

}